#!/usr/bin/python2
#-*- coding:utf-8 -*-

from PIL import Image
from skimage import data, filters, io
import matplotlib.pyplot as plt
import os

'''
get picture and cut to pieces
'''
class Div(object):
    """docstring for Div"""

    # set argvs
    def __init__(self, im):
        super(Div, self).__init__()
        self.im = im # PIL.Image picture
        self.imgry = self.im.convert('L') # greyscale picture
        self.hist = self.im.convert('L').histogram() # grey level
        self.imgbin = None # binary picture after denoise
        self.start = 0

    '''
    division picture function
    frame: mark pixel location
    w: horizontal axis
    h: vertical axis
    cpatcha: pixel of cpatcha
    return each 'single' cpatcha's four coordinate point
    '''
    def divPict(self, frame, w, h, cpatcha = 0):
        print "--------starting division picture--------"
        pieces = []
        start = 0
        end = 256
        
        # scanning weight and high of the cpatcha
        for i in range(w):
            for j in range(h):
                # if cpatcha different with background, self start = 1
                if frame[i, j] == cpatcha:
                    if self.start == 0:
                        self.start = 1
                        start = i
                    break
            # find cpatcha
            if self.start == 1:
                # j == h - 1, means scan cpatcha done
                if j == (h -1):
                    end = i # mark end location
                    self.start = 0 # horizontal scan done for one piece
                    #if (end - start) > w / 10:
                    piece = [start, 0, end, h] # get a piece
                    
                    # start scan vertical axis
                    # same with horizontal scan
                    for k in range(h):
                        for l in range(w):
                            if frame[l, k] == cpatcha:
                                if self.start == 0:
                                    self.start = 1
                                    start = k
                                break
                        if self.start == 1:
                            if l == (w - 1):
                                self.start = 0
                                end = k
                                piece[1] = start
                                piece[3] = end
                                #if end - start > h / 10:
                                pieces.append(piece)
        
        # assume there are four cpatchas
        len_p = len(pieces)
        dist = []
        for piece in pieces:
                dist.append(piece[2] - piece[0])
        print dist
        sum_d = sum(dist)
        max_d = max(dist)
        avg_d = sum_d / len_p
        #print avg_d
        index = dist.index(max_d)
        
        if len_p == 1:
            print '1'
        elif len_p == 2:
            print '2'
        elif len_p == 3:
            print "the picture has benn cut to %s parts:" %str(len_p)
            oth = sum_d - max_d
            if max_d > (sum_d - max_d) :
                newlenth = max_d / 2
                alter = pieces[index]
                del pieces[index]
                #print alter, newlenth
                begin = alter[0] + newlenth
                enddd = alter[2] - newlenth
                #print begin, enddd
                head = alter[:]
                rear = alter[:]
                head[2] = begin
                rear[0] = enddd
                #print head, rear
                pieces.insert(index, head)
                pieces.insert(index + 1, rear)
        else:
            print '4'

        print pieces
        return pieces

    '''
    auto denoise function
    use skimage get thresh
    '''
    def denoiseAuto(self):
        table = []
        self.imgry.save('testgray.png')
        imageo = io.imread('testcolor.png')
        image = io.imread('testgray.png', as_gray = True)
        #print image
        thresh = filters.threshold_otsu(image)   #返回一个阈值
        print thresh
        dst =(image >= thresh)*1.0   #根据阈值进行分割
        plt.figure('thresh', figsize = (8, 8))
        plt.subplot(311)
        plt.title('original image')
        plt.imshow(imageo, plt.cm.gray)
        plt.subplot(312)
        plt.title('gray image')
        plt.imshow(image, plt.cm.gray)
        plt.subplot(313)
        plt.title('binary image')
        plt.imshow(dst, plt.cm.gray)
        plt.show()
        for i in range(256):
            if i <= thresh:
                table.append(0)
            else:
                table.append(1)
        imgPure = self.imgry.point(table, '1')
        print "save denoised picture"
        if imgPure.save('test.png'):
            print "save success"
        self.imgbin = imgPure

    '''
    denoise function
    not a normal way binary a picture
    '''
    def denoiseHand(self, thresholdL = None, thresholdH = None):
        table = []
        self.imgry.save('testgray.png')
        # hint function
        #threshold value should decided by some function
        try:
            # if self.hist[0] != 0:
            #     thresholdL = 0
            #     thresholdH = 2
            # else:
            #     thresholdL = 253
            #     thresholdH = 255
            if thresholdL is None or thresholdH is None:
                thresholdL = input('门限区间的较小值：\n')
                thresholdH = input('门限区间的较大值：\n')
            thresholdL = int(thresholdL)
            thresholdH = int(thresholdH)
            for i in range(256):
                if i >= thresholdL and i <= thresholdH:
                    table.append(0)
                else:
                    table.append(1)
            imgPure = self.imgry.point(table, '1')
            print "save denoised picture"
            if imgPure.save('test.png'):
                print "save success"
            self.imgbin = imgPure
        except:
            print "denoise failed"
            pass

    def reDenoise():
        pass

    '''
    a main division funciton
    test for divPict function
    2016.6.7
    '''
    def mainDivision(self):
        imgPieces = []
        print self.imgbin
        if self.imgbin is None:
            self.denoiseAuto()
        frame = self.imgbin.load()
        (w, h) = self.imgbin.size
        print w, h
        pieces = self.divPict(frame, w, h)
        i = 1
        for piece in pieces:
            im = self.imgbin.crop(piece)
            print "division %s picture done." %i
            im.save('divisioned_%s.png'%i)
            i = i + 1
            imgPieces.append(self.imgbin.crop(piece))
        return imgPieces

def main():
    img = 'testcolor.png'
    im = Image.open(img)
    d = Div(im)
    pieces = d.mainDivision()

if __name__ == '__main__':
    main()